
class TreeNode {
    int val;
     TreeNode left;
      TreeNode right;
     TreeNode() {}
     TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
        this.left = left;
          this.right = right;
     }
 }

public class Test {
    public static void main(String[] args) {

    }
    long prev = Long.MIN_VALUE;
    public boolean isValidBST(TreeNode root) {
        if(root == null) return true;
        boolean left = isValidBST(root.left);
        if(left == false) return false;//剪枝
        boolean cur = false;
        if(root.val > prev) cur = true;
        else return false;//剪枝
        prev = root.val;
        boolean right = isValidBST(root.right);
        return left && cur && right;

    }
    //230.二叉搜索树中的第k小元素
    int cur = 1;
    int dest = 0;
    public int kthSmallest(TreeNode root, int k) {
        dfs(root,k);
        return dest;
    }
    public void dfs(TreeNode root,int k) {
        if(root == null) return;
        dfs(root.left,k);
        if(cur == k) {
            dest = root.val;
            cur++;
            return;
        }
        cur++;
        dfs(root.right,k);
    }
}
